package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.Building.BuildingAddException;
import com.ruoyi.common.exception.Building.BuildingDelByIdException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.dto.CommunityBuildingDto;
import com.ruoyi.system.domain.vo.BuildingVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.CommunityBuildingMapper;
import com.ruoyi.system.domain.CommunityBuilding;
import com.ruoyi.system.service.ICommunityBuildingService;
import org.springframework.transaction.annotation.Transactional;

/**
 * buildingService业务层处理
 * 
 * @author hanpee
 * @date 2024-03-22
 */
@Service
public class CommunityBuildingServiceImpl implements ICommunityBuildingService 
{
    @Autowired
    private CommunityBuildingMapper communityBuildingMapper;

    /**
     * 查询building
     * 
     * @param id building主键
     * @return building
     */
    @Override
    public CommunityBuilding selectCommunityBuildingById(Long id)
    {
        return communityBuildingMapper.selectCommunityBuildingById(id);
    }

    /**
     * 查询building列表
     * 
     * @param communityBuilding building
     * @return building
     */
    @Override
    public List<CommunityBuilding> selectCommunityBuildingList(CommunityBuilding communityBuilding)
    {
        return communityBuildingMapper.selectCommunityBuildingList(communityBuilding);
    }

    /**
     * 新增Building
     * 抛出异常则会回滚
     *
     * @param communityBuilding building
     * @return 结果
     * @throws Exception 异常
     * @Transactional(rollbackFor = Exception.class)
     */
    @Override
    public int insertCommunityBuilding(CommunityBuilding communityBuilding){
//      communityBuilding.setCreateTime(DateUtils.getNowDate());
        BuildingVo c = getByName(communityBuilding.getName());
        if(c == null){
            return communityBuildingMapper.insertCommunityBuilding(communityBuilding);
        }else{
            String name = c.getName();
            try{
                if(!communityBuilding.getName().equals(name)){
                    return communityBuildingMapper.insertCommunityBuilding(communityBuilding);
                }else {
                    throw new BuildingAddException("该楼宇名称已存在！！");
                }
            }catch (BuildingAddException e) {
                throw new BuildingAddException("该楼宇名称已存在！！");
            }
        }
    }

    /**
     * 修改building
     * 
     * @param communityBuilding building
     * @return 结果
     */
    @Override
    public int updateCommunityBuilding(CommunityBuilding communityBuilding) {
            communityBuilding.setUpdateTime(DateUtils.getNowDate());
            return communityBuildingMapper.updateCommunityBuilding(communityBuilding);
    }

    /**
     * 批量删除building
     * 
     * @param ids 需要删除的building主键
     * @return 结果
     */
    @Override
    public int deleteCommunityBuildingByIds(Long[] ids)
    {
        return communityBuildingMapper.deleteCommunityBuildingByIds(ids);
    }

    /**
     * 删除building信息
     * 
     * @param id building主键
     * @return 结果
     */
    @Override
    public int deleteCommunityBuildingById(Long id) {
        boolean a = communityBuildingMapper.watchParams(id);
        if (a) {
            return communityBuildingMapper.deleteCommunityBuildingById(id);
        } else {
            throw new BuildingDelByIdException("当前楼宇下存在单元！！！");
        }
    }


    @Override
    public BuildingVo getByName(String name) {
        return communityBuildingMapper.getByName(name);
    }

    /**
     * 统计图查询楼宇房间数
     * @param communityBuildingDto CommunityBuilding类
     * @return List集合
     */
    @Override
    public List<CommunityBuildingDto> listRooms(CommunityBuildingDto communityBuildingDto) {
        return communityBuildingMapper.listRooms(communityBuildingDto);
    }

    /**
     * 查询所有的楼宇
     * @return List集合
     */
    @Override
    public List<CommunityBuildingDto> listBuilding() {
        return communityBuildingMapper.listBuilding();
    }

    /**
     * 级联删除（判断当前楼宇是否存在单元）
     * @param id 楼宇Id
     * @return boolean，如果为true则为删除成功，否则删除失败
     */
    @Override
    public boolean watchParams(Long id) {
        return communityBuildingMapper.watchParams(id);
    }
}
